<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	StringBuilderEx sql = null;
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		sql = new StringBuilderEx();
	}
	
	protected void EndProcess() throws Exception{
		if(form.ationEq("save,delete")){
			onSearchRetrieveResult();
		}
	}
	
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		if(!form.isNullOrEmpty("pan_subpro_menusn")){
			rs = utils.executeQuery("info5","select * from pan..pan_subpro_menu where sn = '"+form.gpsv("pan_subpro_menusn")+"'");
			if(rs.next()){
				form.FromResultSet(rs, false);
				if("0".equalsIgnoreCase(form.gpsv("enab"))){
					ret.setWarningInfo("此單已取消!");
					page.getToolbar().disableAllButtonExcept("newnext");
				}
			}else{
				ret.setWarningInfo("此單已刪除,如需要請重新新增!");
				page.getToolbar().disableAllButtonExcept("newnext");
			}
			onSearchRetrieveResult();
		}
		return true ;
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{
		String sqlhrnums = "''";		
		//已選工號
		if(!form.isNullOrEmpty("hrnums")){
			sqlhrnums = StringHelper.arrayToString(form.gpsv("hrnums").split(","), "'", ",");
			rs = utils.executeQuery("info5", StringHelper.Format("select b.hrnum,b.chname,c.ChFullName as hr003name,d.companyNameCh as hr003aname "
				+" from hr..hr005 b ,hr..hr003 c ,hr..hr003a d"
				+" where b.fhr003tsn = c.tsn and c.hr003atsn = d.tsn and b.hrnum in(%1$s) order by d.companyNameCh,c.ChFullName ",sqlhrnums));
			ret.spov("rs1", rs);
		}
		
		//可選工號
		rs = utils.executeQuery("info5", StringHelper.Format("select distinct a.hrnum,a.chname,c.ChFullName as hr003name,d.companyNameCh as hr003aname "
			+" from pan..PAN_SUBPRO_APPHRNUM a,hr..hr005 b ,hr..hr003 c ,hr..hr003a d"
			+" where a.hrnum = b.hrnum and b.fhr003tsn = c.tsn and c.hr003atsn = d.tsn and a.status = 2 and a.hrnum not in(%1$s) order by d.companyNameCh,c.ChFullName",sqlhrnums));
		ret.spov("rs2", rs);
	}
	
	protected void doSave() throws Exception{
		sql.Reset();
		String hrnums[] = form.gpsvs("key2");
		int icode = 0; 
		String strHrnums = form.gpsv("hrnums");
		if(hrnums.length>0 || !form.isNullOrEmpty("hrnum2")){
			for(int i= 0;i<hrnums.length;i++){
				if((","+strHrnums+",").indexOf(","+hrnums[i]+",") == -1){
					if(strHrnums.length()>0){
						strHrnums += ",";
					}
					strHrnums += hrnums[i];
				}
			}
			
			if(!form.isNullOrEmpty("hrnum2")){
				if((","+strHrnums+",").indexOf(","+form.gpsv("hrnum2")+",") == -1){
					if(strHrnums.length()>0){
						strHrnums += ",";
					}
					strHrnums += form.gpsv("hrnum2");
				}
				form.RemoveParam("hrnum2");
				form.RemoveParam("chname2");
			}
			
			if(!strHrnums.equals(form.gpsv("hrnums"))){
				//1.儲存
				CrudDataEntity crud = form.getCrudDataEntity("info5","pan..pan_subpro_menu","sn");
				crud.spsv("sn", form.gpsv("pan_subpro_menusn"));
				crud.setSpportSimple(true);
				crud.spsv("hrnums", strHrnums);
				crud.appendUpdateCols();
				icode = crud.save();
			}
		}
		
		if(icode > 0){
			form.spsv("hrnums", strHrnums);
			ret.setOkInfo("存儲成功!");
		}
	}
	
	protected void doDelete() throws Exception{
		sql.Reset();
		String hrnums[] = form.gpsvs("key1");
		int icode = 0; 
		String strHrnums = form.gpsv("hrnums");
		if(hrnums.length>0){
			strHrnums = ","+strHrnums+",";
			for(int i= 0;i<hrnums.length;i++){
				strHrnums = strHrnums.replaceAll(","+hrnums[i]+",", ",");
			}
			if(!strHrnums.equals(",")){
				strHrnums = strHrnums.substring(1,strHrnums.length()-1);
			}else{
				strHrnums = "";
			}
			
			if(!strHrnums.equals(form.gpsv("hrnums"))){
				//1.儲存
				CrudDataEntity crud = form.getCrudDataEntity("info5","pan..pan_subpro_menu","sn");
				crud.spsv("sn", form.gpsv("pan_subpro_menusn"));
				crud.setSpportSimple(true);
				crud.spsv("hrnums", strHrnums);
				crud.appendUpdateCols();
				icode = crud.save();
			}
		}
		
		if(icode > 0){
			form.spsv("hrnums", strHrnums);
			ret.setOkInfo("刪除成功!");
		}
	}

}
%>
<%@include file="/include/common/gridbackend.jsp"%>